import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vue from '@vitejs/plugin-vue'
import { createHtmlPlugin } from 'vite-plugin-html'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import UnoCSS from 'unocss/vite'


export default defineConfig(({mode}) => {

    const APP_ENV = loadEnv(mode, process.cwd(), 'VITE_APP')

    return {
        plugins: [
            vue(),
            UnoCSS(),
            vueJsx(),
            createHtmlPlugin({
                minify: true,
                inject: {
                    data: APP_ENV
                }
            }),
            Components({
                resolvers: [NaiveUiResolver()],
                dts: 'types/auto-components.d.ts'
            }),
            AutoImport({
                imports: [
                    'vue',
                    'vue-router',
                    'pinia',
                    {
                        'naive-ui': [
                            'useDialog',
                            'useMessage',
                            'useNotification',
                            'useLoadingBar'
                        ]
                    }
                ],
                eslintrc: {
                  // 已存在文件设置默认 false，需要更新时再打开，防止每次更新都重新生成
                  enabled: true,
                  // 生成文件地址和名称
                  filepath: 'types/.eslintrc-auto-imports.json',
                  globalsPropValue: true,
                },
                dts: 'types/auto-imports.d.ts'
            })
        ],
        define: {
            __APP_ENV__: APP_ENV
        },
        resolve: {
            alias: {
                '@': path.resolve(__dirname, 'src')
            }
        },
        server: {
            host: '0.0.0.0',
            proxy: {}
        }
    }
})
